package com.train.common.service.impl;

import com.train.common.service.MinioService;
import com.train.common.utils.file.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * MinIO服务实现类
 */
@Service
public class MinioServiceImpl implements MinioService {

    @Autowired
    private MinioUtils minioUtils;


    @Override
    public String uploadFile(MultipartFile file, String prefix) {
        // 使用自动生成文件名的方法
        return minioUtils.uploadWithAutoFilename(file, prefix);
    }

    @Override
    public void deleteFile(String objectName) {
        minioUtils.delete(objectName);
    }

    @Override
    public String getPresignedUrl(String objectName, int expires) {
        return minioUtils.getPresignedObjectUrl(objectName, expires);
    }

    @Override
    public Map<String, String> getUploadCredentials(String originalFilename, String prefix) {
        // 生成对象名
        String objectName = minioUtils.generateObjectName(originalFilename, prefix);

        // 获取预签名上传URL
        String uploadUrl = minioUtils.getPresignedPutObjectUrl(objectName);

        Map<String, String> result = new HashMap<>();
        result.put("uploadUrl", uploadUrl);
        result.put("objectName", objectName);

        return result;
    }

    @Override
    public String getPermanentUrl(String objectName) {
        return minioUtils.getPermanentUrl(objectName);
    }
}